#include <QMessageBox>
#include <QFileDialog>
#include <QDataStream>
#include <QScrollBar>
#include <QImage>

#include "window/Window.hpp"
#include "window/Editor.hpp"
#include "window/NewFile.hpp"

NewFile::NewFile(QWidget* parent) : QDialog(parent){
    this->layout = new QVBoxLayout();

    this->nameLayout = new QHBoxLayout();
    this->nameInput  = new QLineEdit();

    this->scrollArea = new QScrollArea();
    this->imagesManager = new ImagesManager(this->scrollArea);

    this->buttonLayout = new QHBoxLayout();
    this->acceptButton = new QPushButton("确认");
    this->cancelButton = new QPushButton("取消");

    this->scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->scrollArea->setWidget(this->imagesManager);

    this->nameInput->setPlaceholderText("文件名称");
    this->nameInput->setAlignment(Qt::AlignHCenter);

    this->nameLayout->addStretch();
    this->nameLayout->addWidget(this->nameInput);
    this->nameLayout->addStretch();

    this->buttonLayout->addStretch();
    this->buttonLayout->addWidget(this->cancelButton);
    this->buttonLayout->addWidget(this->acceptButton);

    this->layout->addLayout(this->nameLayout);
    this->layout->addWidget(this->scrollArea);
    this->layout->addLayout(this->buttonLayout);

    this->setLayout(layout);

    QObject::connect(this->cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
    QObject::connect(this->acceptButton, SIGNAL(clicked()), this, SLOT(accept()));

    // 设置窗口关闭后释放
    this->setAttribute(Qt::WA_DeleteOnClose);
    
}

NewFile::~NewFile(){
    delete this->nameInput;

    delete this->imagesManager;
    delete this->scrollArea;

    delete this->acceptButton;
    delete this->cancelButton;

    delete this->nameLayout;
    delete this->buttonLayout;
    
    delete this->layout;

}

void NewFile::cancel(){
    this->close();

}

void NewFile::accept(){
    QString filename = this->nameInput->text();
    int count  = this->imagesManager->files.size();

    if(filename.compare("") == 0 || count == 0){
        QMessageBox::warning(this, "警告", "不输入名称或不导入图片的情况下无法保存！");

    }else{
        //选择文件保存位置
        QString savefile = QFileDialog::getExistingDirectory(this, "保存", "./");

        if(savefile.compare("") != 0){
            CYFile* file = new CYFile(savefile.append(QString("/%1.cy").arg(filename)));

            for(int i = 0; i < count; i++){
                Image data = this->imagesManager->images.at(i);
                file->addImage(data.getImage());

            }

            file->saveFile();

            Window* window = (Window*)this->parentWidget();
            // window->setCYFile(file);

            // // 关闭新建界面
            // this->setParent(nullptr);
            // this->hide();

            // delete this;

            this->close();

            emit addTab(file);

            // Editor* editor = new Editor(file);

            // window->setCentralWidget(editor);
            // editor->show();

        }

    }

}
